package cn.edu.zjut.chopper.registry.servlet;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.edu.zjut.chopper.constant.Constant;
import cn.edu.zjut.chopper.message.RemoteInterface;
import cn.edu.zjut.chopper.name.NameServer;
import cn.edu.zjut.chopper.registry.RegistryContext;

/**
 * Servlet implementation class RemoteInterfaceNameView
 */
public class RemoteInterfaceNameView extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static Log LOG = LogFactory.getLog(RemoteInterfaceNameView.class);
	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RemoteInterfaceNameView() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		NameServer nameServer = (NameServer)RegistryContext.get(Constant.NameServer);
		if(nameServer == null){
			getServletContext().getRequestDispatcher("/error.jsp").forward(
					request, response);
			if(LOG.isErrorEnabled()){
				LOG.error("can not get NameServer Instance.");
			}
			return;
		}
		Map<String, Set<RemoteInterface>> remoteInterfaces = nameServer.list();
		request.setAttribute("remoteInterfaces", remoteInterfaces);
		getServletContext().getRequestDispatcher("/remote_interface_name_view.jsp").forward(
				request, response);

	}

}
